package com.mzp.XbShop.service;

import com.mzp.XbShop.common.entity.Result;
import com.mzp.XbShop.common.util.IdWorker;
import com.mzp.XbShop.mapper.GoodsDao;
import com.mzp.XbShop.pojo.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author admin
 * @Description goods 服务层
 * @date 2021-04-17 15:04:51
 */
@Service
public class GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<Goods> findAll() {
        return goodsDao.findAll();
    }

    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<Goods> findSearch(Map whereMap, int page, int size) {
        Specification<Goods> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return goodsDao.findAll(specification, pageRequest);
    }

    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<Goods> findSearch(Map whereMap) {
        Specification<Goods> specification = createSpecification(whereMap);
        return goodsDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public Goods findById(String id) {
        return goodsDao.findById(id).get();
    }

    /**
     * 增加
     *
     * @param goods
     */
    public void add(Goods goods) {
        goods.setId(idWorker.nextId() + "");
        goodsDao.save(goods);
    }

    /**
     * 修改
     *
     * @param goods
     */
    public void update(Goods goods) {
        goodsDao.save(goods);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        goodsDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<Goods> createSpecification(Map searchMap) {

        return new Specification<Goods>() {

            @Override
            public Predicate toPredicate(Root<Goods> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                //title
                if (searchMap.get("title") != null && !"".equals(searchMap.get("title"))) {
                    predicateList.add(cb.like(root.get("title").as(String.class), "%" + (String) searchMap.get("title") + "%"));
                }
                //desc
                if (searchMap.get("desc") != null && !"".equals(searchMap.get("desc"))) {
                    predicateList.add(cb.like(root.get("desc").as(String.class), "%" + (String) searchMap.get("desc") + "%"));
                }
                //price
                if (searchMap.get("price") != null && !"".equals(searchMap.get("price"))) {
                    predicateList.add(cb.like(root.get("price").as(String.class), "%" + (String) searchMap.get("price") + "%"));
                }
                //publishDate
                if (searchMap.get("publishDate") != null && !"".equals(searchMap.get("publishDate"))) {
                    predicateList.add(cb.like(root.get("publishDate").as(String.class), "%" + (String) searchMap.get("publishDate") + "%"));
                }
                //imagesId
                if (searchMap.get("imagesId") != null && !"".equals(searchMap.get("imagesId"))) {
                    predicateList.add(cb.like(root.get("imagesId").as(String.class), "%" + (String) searchMap.get("imagesId") + "%"));
                }
                //mainimage
                if (searchMap.get("mainimage") != null && !"".equals(searchMap.get("mainimage"))) {
                    predicateList.add(cb.like(root.get("mainimage").as(String.class), "%" + (String) searchMap.get("mainimage") + "%"));
                }
                //status
                if (searchMap.get("status") != null && !"".equals(searchMap.get("status"))) {
                    predicateList.add(cb.like(root.get("status").as(String.class), "%" + (String) searchMap.get("status") + "%"));
                }
                //repertory
                if (searchMap.get("repertory") != null && !"".equals(searchMap.get("repertory"))) {
                    predicateList.add(cb.like(root.get("repertory").as(String.class), "%" + (String) searchMap.get("repertory") + "%"));
                }
                //groupId
                if (searchMap.get("groupId") != null && !"".equals(searchMap.get("groupId"))) {
                    predicateList.add(cb.like(root.get("groupId").as(String.class), "%" + (String) searchMap.get("groupId") + "%"));
                }
                //visit
                if (searchMap.get("visit") != null && !"".equals(searchMap.get("visit"))) {
                    predicateList.add(cb.like(root.get("visit").as(String.class), "%" + (String) searchMap.get("visit") + "%"));
                }

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    public Goods[] newlist() {
        return goodsDao.findTop4ByStatusOrderByPublishdateDesc("0");
    }


    public List<Goods> hotlist() {
        return goodsDao.findTop4ByStatusOrderByVisitDesc("0");
    }

    public List<Goods> findByGroupId(String categoryNum) {
        return goodsDao.findBygroupid(categoryNum);
    }

    public List<Goods> findByGroupid(String groupid) {
        return goodsDao.findBygroupid(groupid);
    }

    public List<Goods> findByTitle(String title) {
        return goodsDao.findByTitleLike(title);
    }

    @Transactional
    public Integer updateVisitors(String id) {
        return goodsDao.updateVisitors(id);
    }
}
